// pages/updatepassword/index.js
const app=getApp();
const config=app.globalData;
Page({

    /**
     * 页面的初始数据
     */
    data: {
      imgurl:'',
      phone:'',
      countdown:60,
      isStop:true,
      timer:null,
      sessionId: '',

      showPopup: false,
      popupMsg: '',
    },
    // 获取手机号
    getPhone(e){
      this.setData({
        phone:e.detail.value
      })
    },
    // 获取验证码
    getWord(e){
      let that = this
      var phone = that.data.phone;
      if(phone.length==0){
          that.setData({showPopup: true, popupMsg: '电话号码不能为空!'})
          return;
      }
      if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){ 
          that.setData({showPopup: true, popupMsg: "手机号码不正确，请重新输入！"})
          return;
      }
      that.setData({
        countdown:60
      })
      wx.request({
        url: config.url+'api/account/getCode',
        method:'POST',
        data:{phone: phone},
        success(res){
          if(res.data.code === 0){
            that.setData({
                sessionId: res.data.data
            })
            wx.showToast({
              title: '获取验证码成功',
              icon: 'success',
              duration: 1000
            })
            that.data.timer=setInterval(()=>{
              if(that.data.countdown>1){
                that.setData({
                  countdown:that.data.countdown-1,
                  isStop:false
                })
              }else{
                clearInterval(that.data.timer)
                that.setData({
                  isStop:true
                })
              }
            },1000)
          }else{
            that.setData({showPopup: true, popupMsg: res.data.msg})
          }
        }
      })
    },
    formSubmit(e){
        let that = this;
        var phone=e.detail.value.phone;
        var verification=e.detail.value.verification;
        var password=e.detail.value.password;
        var surepassword=e.detail.value.surepassword;
        if(phone.length==0){
            that.setData({showPopup: true, popupMsg: '电话号码不能为空!'})
            return;
        }
        if(verification.length==0){
            that.setData({showPopup: true, popupMsg: '验证码不能为空!'})
            return;
        }
        if(password.length==0){
            that.setData({showPopup: true, popupMsg: '密码不能为空!'})
            return;
        }
        if(surepassword.length==0){
            that.setData({showPopup: true, popupMsg: '确认密码不能为空!'})
            return;
        }
        if(password.length<6||password.length>8){
            that.setData({showPopup: true, popupMsg: '密码必须是6-8个字符!'})
            return;
        }
        if(surepassword!=password){
            that.setData({showPopup: true, popupMsg: '密码输入不一致!'})
            return;
        }
        if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone))){ 
            that.setData({showPopup: true, popupMsg: "手机号码不正确，请重新输入！"})
            return;
        }
        if(! that.data.sessionId){
            that.setData({showPopup: true, popupMsg: '请先获取手机验证码!'})
            return;
        }
        wx.request({
          url: config.url+'api/account/forgetPwd',
          header:{
            'SESSIONID': that.data.sessionId
          },
          data:{
            phone:phone,
            psw:password,
            code:verification
          },
          method:'POST',
          success(res){
            if(res.data.code === 0){
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 1000,
                    success(data) {
                        setTimeout(function () {
                            wx.navigateTo({
                                url: '../../pages/login/index',
                            })
                        }, 1000) //延迟时间
                    }
                })
            }else{
                that.setData({showPopup: true, popupMsg: res.data.msg})
            }
          }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.setData({
        imgurl:config.imgurl
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
      clearInterval(this.data.timer)
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      clearInterval(this.data.timer)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})